﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.UI.HtmlControls;
using BSCMFF.Web.UserControls;
using BSCMFF.Core.DataAccessLayer;
using BSCMFF.Core.BusinessLayer;

using System.Drawing;


namespace BSCMFF.Web.Pages
{
    public partial class WizardBusinessTarget : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Wizard1.ActiveStepIndex = 0;

                Wizard1.BackColor = ColorTranslator.FromHtml("#E9EFF8");
            }
        }

      //  private static Table ptMeasures; // = new Table();
        protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            LabelActiveIndex.Text = Wizard1.ActiveStepIndex.ToString();

            //if (Wizard1.ActiveStepIndex == 0)
            //{
            //    RadButton next = (RadButton)Wizard1.FindControl("StepNavigationTemplateContainerID").FindControl("StepNextButton");
            //    next.CausesValidation = false;
            //}
            //from solutions to measures
            if (Wizard1.ActiveStepIndex == 1)
            {
                int j = 0;

               // Table tt = TableMeasures;// Wizard1.WizardSteps[2].FindControl("TableMeasures") as HtmlTable;


                foreach (Control c in Wizard1.WizardSteps[1].Controls)
                {
                    if (c.GetType() == typeof(RadTextBox) )
                    {

                        for (int i = 0; i < 3; ++i)
                        {
                            if (((RadTextBox)c).Text != "")
                            {
                                // 3 possible measures for each solution
                                ((Label)TableMeasures.Controls[j * 3 + i + 1].Controls[0].Controls[0]).Text = ((RadTextBox)c).Text;
                                TableMeasures.Controls[j * 3 + i + 1].Visible = true;
                            }
                            else
                            {//hide measures for empty solution
                                ((Label)TableMeasures.Controls[j * 3 + i + 1].Controls[0].Controls[0]).Text = "";
                                TableMeasures.Controls[j * 3 + i + 1].Visible = false;
                            }
                        }

                        ++j;
                    }
                }
            }


            //from measures to measure perspective
            if (Wizard1.ActiveStepIndex == 2)
            {
                for (int i = 1; i < TableMeasures.Controls.Count; ++i)
                {
                    if (((RadTextBox)TableMeasures.Controls[i].Controls[1].Controls[0]).Text != "")
                    {
                        ((Label)TablePerspectives.Controls[i].Controls[0].Controls[0]).Text = ((Label)TableMeasures.Controls[i].Controls[0].Controls[0]).Text;
                        ((Label)TablePerspectives.Controls[i].Controls[1].Controls[0]).Text = ((RadTextBox)TableMeasures.Controls[i].Controls[1].Controls[0]).Text;
                        TablePerspectives.Controls[i].Visible = true;
                    }
                    else
                    {
                        TablePerspectives.Controls[i].Visible = false;
                    }
                }
            }

            //from perspectives to summary
            if (Wizard1.ActiveStepIndex == 3)
            {
                SummaryBusinessTarget.Text = RadTextBoxBusinessTarget.Text;

                for (int i = 1; i < TableSummary.Controls.Count; ++i)
                {
                    if (((RadTextBox)TableMeasures.Controls[i].Controls[1].Controls[0]).Text != "")
                    {
                        ((Label)TableSummary.Controls[i].Controls[0].Controls[0]).Text = ((Label)TableMeasures.Controls[i].Controls[0].Controls[0]).Text;
                        ((Label)TableSummary.Controls[i].Controls[1].Controls[0]).Text = ((RadTextBox)TableMeasures.Controls[i].Controls[1].Controls[0]).Text;
                        ((Label)TableSummary.Controls[i].Controls[2].Controls[0]).Text = ((SelectionPerspective)TablePerspectives.Controls[i].Controls[2].Controls[0]).SelectedPerspective;
                        TableSummary.Controls[i].Visible = true;
                    }
                    else
                    {
                        TableSummary.Controls[i].Visible = false;
                    }
                }
            }
        }

        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            BSCEntities context = new BSCEntities();

            Strategic_Target st = new Strategic_Target();
            st.Name = RadTextBoxBusinessTarget.Text;
            st.Description = RadTextBoxDescription.Text;
            st.Perspective_FK = SelectionPerspectiveTarget.SelectedPerspectiveID;
            st.ExpectedDate = RadDatePickerTargetExpected.SelectedDate;

            Strategy_Node sn = new Strategy_Node();
            sn.Positioned = false;
            sn.Strategy_Map_FK = 1; // only one map is used (for now)
            st.Strategy_Node.Add(sn);

            List<Business_Solution> solutions = new List<Business_Solution>();
            
            //OLSolutions.Controls[
            ////foreach (TableRow r in tableso
            
            //List<KPI> kpis = new List<KPI>();

            KPIManager kpim = new KPIManager();

            bool isHeader = true;
            Business_Solution bs = new Business_Solution();
            foreach (TableRow r in TableSummary.Rows)
            {
                if (isHeader == false && r.Visible == true)
                {
                    string solution = ((Label)r.Controls[0].Controls[0]).Text;
                    

                    if (solutions.Count == 0 || (solutions.Count > 0 && solutions[solutions.Count - 1].Solution != solution))
                    {
                        bs = new Business_Solution();
                        bs.Solution = solution;
                        st.Business_Solution.Add(bs);
                        //bs.Strategic_Target_FK = st.Strategic_Target_ID;
                        solutions.Add(bs);
                        //context.AddToBusiness_Solution(bs);

                    }

                    string measure = ((Label)r.Controls[1].Controls[0]).Text;

                    KPI kpi = new KPI();
                    kpi.Name = measure;
                    //kpi.Solution_FK = bs.ID;
                    string perspective = ((Label)r.Controls[2].Controls[0]).Text;
                    kpi = kpim.SetKPIPerspective_FK(kpi, perspective);
                    bs.KPIs.Add(kpi);
                    //kpis.Add(kpi);
                }
                else
                {
                    isHeader = false;
                }
            }


            context.AddToStrategic_Target(st);

            try
            {
                context.SaveChanges();
                LabelResult.Text = "Data were successfully saved !";
                LabelResult.ForeColor = System.Drawing.Color.Green;
            }
            catch
            {
                LabelResult.Text = "There was a problem with saving data !";
                LabelResult.ForeColor = System.Drawing.Color.Red;
                //throw new Exception();
            }
        }

      /*  Label SideBarInfo;
 
// Hide all LinkButton controls and duplicate into labels!
        protected void LoadSideBar(object s, EventArgs e)
        {
            //InsertImages(s);
        }

        private void InsertImages(object s)
        {
            DataList me = (DataList)s;
            // Reset label (postbacks)
            //SideBarInfo.Text = "";

            int step = 0;
            SideBarInfo.Text = "";
            // Loop through datalistitems
            foreach (DataListItem item in me.Items)
            {
                // Each list item
                LinkButton btn = (LinkButton)item.FindControl("SideBarButton");

                if (btn != null)
                {
                    Label newLabel = new Label();
                    //newLabel.Text = "<asp:Image ID='imgb' runat='server' ImageUrl='~/Images/wizard green arrow.jpg' AlternateText='tralal'/>"; //btn.Text;
                    newLabel.Text = "<img src='../../Images/Business Target Wizard/bw" + step.ToString() + ".png' alt='poskus'/>";

                    //Image i = new Image();
                    //i.ID = "image" + i.ToString();
                    //i.ImageUrl = "~/Images/Business Target Wizard/" + i.ToString() + ".png";


                    // Current step?
                    if (me.SelectedItem == item)
                    {
                        newLabel.CssClass = me.SelectedItemStyle.CssClass;
                        newLabel.Text = "<img src='../../Images/Business Target Wizard/" + step.ToString() + ".png' alt='poskus'/>";
                    }
                    // Styled divs
                    SideBarInfo.Text += "<div class='" + newLabel.CssClass + "'>" + newLabel.Text + "</div>";

                    ++step;
                }
            }
        }
        protected void InitSideBarLbl(object s, EventArgs e)
        {
           SideBarInfo = (Label)s;

        }*/

        protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
        {
            Wizard1.HeaderText = "<h2>" + Wizard1.ActiveStep.Title + "</h2>";
        }

        protected void RadButtonCreateNewTarget_Click(object sender, EventArgs e)
        {
            //RadButtonCreateNewTarget.PostBackUrl = Request.Url.ToString();
            Response.Redirect(Request.Url.ToString());
        }

        protected void RadButtonShowProgress_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/BSC_Setup/BSCTreeList.aspx");
            //RadButtonShowProgress.PostBackUrl = "~/Pages/BSC_Setup/BSCTreeList.aspx";
        }

        
        
    }


}       